
#include "board_adc.h"

static volatile uint16_t ADC2ConvertedValue;

/**
  * @brief  Init all neccessary for ADC.
  * @param  None.
  * @retval None
  */
void adc_init(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
  NVIC_InitTypeDef NVIC_InitStructure;
  ADC_InitTypeDef ADC_InitStructure;

  /* GPIO init. */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

  /* Configure PA04(ADC Channel4)as analog inputs */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
  GPIO_Init(GPIOA, &GPIO_InitStructure);


  /* Interrupt init. */
  /* Configure and enable ADC interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = ADC1_2_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = ADC_PRIORITY_GROUP;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = ADC_SUB_PRIORITY;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);

  /* ADC2 configuration ------------------------------------------------------*/
  /* ADCCLK = PCLK2/4 */
  RCC_ADCCLKConfig(RCC_PCLK2_Div4);
  /* Enable ADC1, ADC2, ADC3 and GPIOC clocks */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC2, ENABLE);

  ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;// ADC mode
  ADC_InitStructure.ADC_ScanConvMode = DISABLE;//scanning of groupe of channels disable
  ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;//continuous convertion enable
  ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//Align
  ADC_InitStructure.ADC_NbrOfChannel = 1;//number of channels in the groupe
  ADC_Init(ADC2, &ADC_InitStructure);
  /* ADC2 regular channel4 configuration */
  ADC_RegularChannelConfig(ADC2, ADC_Channel_4, 1, ADC_SampleTime_239Cycles5);
  /*
   ADC_InitStructure.ADC_ScanConvMode  should be enabled
  ADC_InitStructure.ADC_NbrOfChannel   shuold be 3
  ADC_RegularChannelConfig(ADC2, ADC_Channel_4, 1, ADC_SampleTime_239Cycles5);
  ADC_RegularChannelConfig(ADC2, ADC_Channel_4, 2, ADC_SampleTime_239Cycles5);
  ADC_RegularChannelConfig(ADC2, ADC_Channel_4, 3, ADC_SampleTime_239Cycles5);

  */


  /* Enable ADC2 EOC interrupt */
  ADC_ITConfig(ADC2, ADC_IT_EOC, ENABLE);


   /* Enable ADC2 */
  ADC_Cmd(ADC2, ENABLE);

  /* Enable ADC2 reset calibration register */
  ADC_ResetCalibration(ADC2);
  /* Check the end of ADC2 reset calibration register */
  while(ADC_GetResetCalibrationStatus(ADC2));

  /* Start ADC2 calibration */
  ADC_StartCalibration(ADC2);
  /* Check the end of ADC2 calibration */
  while(ADC_GetCalibrationStatus(ADC2));
  /* Start ADC2 Software Conversion */
  ADC_SoftwareStartConvCmd(ADC2, ENABLE);
}

/**
  * @brief  ADC interrupt handler.
  * @param  None.
  * @retval None
  */
void ADC1_2_IRQHandler(void)
{
  /* Get injected channel4 converted value */
  ADC2ConvertedValue = ADC_GetConversionValue(ADC2);
}

/* Just return adc value. */
uint16_t get_adc_value(void)
{
  return(ADC2ConvertedValue);
}


